package sk.fei.vrml.objects;

import sk.fei.vrml.main.Constants;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.glu.GLU;
import javax.media.opengl.glu.GLUquadric;
/**
 * VrmlSphere class represents VRML object Sphere. Only property is radius.
 * 
 * http://www.lighthouse3d.com/vrml/tutorial/index.shtml?sphere
 * 
 *	Sphere {
 *			field SFFloat radius 1
 *	}
 * 
 * @author anubis
 * 
 */
public class VrmlSphere extends VrmlObject {
	private float radius;

	public VrmlSphere(float radius) {
		this.radius = radius;
		this.type = Constants.Sphere;
	}
	
	public void draw(GLAutoDrawable GLAutoDrawable,VrmlAppearance appearance) {
		GL gl = GLAutoDrawable.getGL();
		GLU glu = new GLU();
        GLUquadric sphere = glu.gluNewQuadric();
        glu.gluQuadricDrawStyle(sphere, GLU.GLU_FILL);
        glu.gluQuadricNormals(sphere, GLU.GLU_SMOOTH);
        glu.gluQuadricOrientation(sphere, GLU.GLU_OUTSIDE);
        final int slices = 32;
        final int stacks = 32;
        if(appearance != null) {
	        appearance.applyObjectMaterial(GLAutoDrawable);
	        appearance.applyTexture(GLAutoDrawable, sphere);
        }
        glu.gluSphere(sphere, radius, slices, stacks);
        glu.gluDeleteQuadric(sphere);
        if(appearance != null ) {
        	if (appearance.isTextureSeted()) gl.glDisable(GL.GL_TEXTURE_2D);
        }
	}

	public String toString() {
		return "Sphere";
	}
	
	public float getRadius(){
		return radius;
	}
	
	public void setRadius(float radius){
		this.radius = radius;
	}
}

